<?

function prefather($con){
	global $db; global $tablepre;
	$prefather = $db->select("SELECT name FROM ".$tablepre."stock_type where tid = '$con'");
	return $prefather[0][0];
	}
//上级目录
function prefather_id($con){
	global $db; global $tablepre;
	$prefather = $db->select("SELECT father FROM ".$tablepre."stock_type where tid = '$con'");
	return $prefather[0][0];
	}
//上级目录
function attachment($con0,$con1,$con2){
	global $db; global $tablepre;
	$arr_list_attachment = $db->select("SELECT value1,value2 FROM `".tablepre."stock_attachment` where sid = '$con1' and type = '$con2'");
	return $con0==1? $arr_list_attachment[0][0]:$arr_list_attachment[0][1];
	}
//附加信息
function stock($con1,$con2){
	global $db; global $tablepre;
	$stock = $db->select("SELECT sid,name FROM `".tablepre."stock` where sid = '$con1'");
	return $stock[0][$con2];
	}
	//商品信息
function type($con0){
	global $db; global $tablepre;
	$type = $db->select("SELECT name FROM `".tablepre."stock_type` where tid = '$con0'");
	return $type[0][0];
	}
//分类id
function parameters($con1){
	global $tablepre; global $db;
	$result = $db->select("SELECT value FROM `".tablepre."config` where id = '$con1'");
		return $result[0][0];
	}
//系统信息
function mamber($con1,$con2){
	global $tablepre; global $db;
	$result = $db->select("SELECT * FROM `".tablepre."user` where uid = $con1");
		return $result[0][$con2];
	}
//会员
//$mr = parameters($con1=9);
function mround($con1){
	return round($con1, parameters($con1=9));
	}
	//四舍五入
function price_descount($userid,$price){
	global $db; global $tablepre;
	$vip = $db->select("SELECT discount FROM `".tablepre."vip` where vid = '".mamber($userid,4)."'");
	return mround($price*($vip[0][0]/100));
	}
//计算订单里总共多少钱
function vip($userid,$return){
	global $db; global $tablepre;
	$vip = $db->select("SELECT * FROM `".tablepre."vip` where vid = '".mamber($userid,4)."'");
	return $vip[0][$return];
	}
	//会员
function stock_limit($amount){
	global $db; global $tablepre;
	if($amount<=parameters(13)){
		return $amount;
		}else{
			return parameters(13);
			}
	}
//当库存大于此数时显示约摸
function property($con1,$con2,$con3){
	global $db; global $tablepre;
	
	$quality = $db->select("SELECT * FROM `".$tablepre."tpl` where `type` = '".$con2."'");
		eval("\$arr=".$quality[0][2]."");
		if($con1==1){
			$html.='<label for="TPL_'.$con2.'">'.$con2.'：</label>';
			$html.='<select name="TPL_'.$con2.'">';
			if($con3==null){
				$html.='<option selected="selected">none</option>';
				}else{
					$html.='<option selected="selected" value="'.attachment(1,$con3,'TPL_'.$con2).'">'.$arr[attachment(1,$con3,'TPL_'.$con2)].'</option>';
					}
			
			
			foreach ($arr as $key => $value) {
				$html .='<option value="'.$key.'">'.$value.'</option>';
				}
			$html.='</select>';
			}elseif($con1==2){
				foreach ($arr as $key => $value) {
					$html .='<label for="TPL_'.$key.'">'.$key.'：</label>';
					$html.='<select name="TPL_'.$key.'">';
					if($con3==null){
						$html.='<option selected="selected">none</option>';
						}else{
							$html.='<option selected="selected" value="'.attachment(1,$con3,'TPL_'.$key).'">'.$value[attachment(1,$con3,'TPL_'.$key)].'</option>';
							}
							
						foreach ($value as $key2 => $value2) {
							$html .='<option value="'.$key2.'">'.$value2.'</option>';
							}
							$html.='</select>';
				}
		}elseif($con1=='out_put'){
				if($con3==1){
				$html.='TPL_'.$quality[0][1];
				}elseif($con3==2){
					foreach ($arr as $key => $value) {
						$html.='TPL_'.$key.',';
						}
						$html = substr($html,0,-1);
					}
			}
		

		return $html;
	}
	//属性
function arr_show_li($style,$arr,$v1,$v2,$v3){
	eval("\$str = ".$arr."");
	if($style==1){
	foreach ($str as $key => $value) {
		$arr_print .= '<li><a href="'.$value.'">'.$key.'</a></li>';
	}}elseif($style==2){
		echo $str[1].$v1.$str[2].$v2.$v3;
		}
	
	return $arr_print;
	}
//读变量
function nav($fid=0,$lever=1){
	global $db; global $tablepre;
	$nav = $db->select("SELECT tid,father,name FROM `".$tablepre."stock_type` where `tid` = '".$_GET[tid]."'");
	$current= '<a href="?tid='.$nav[0][0].'">'.$nav[0][2].'</a>';
	
echo NAV;
		function loop($v){
			if($v<>null){
				
				global $db; global $tablepre;
				$nav = $db->select("SELECT tid,father,name FROM `".$tablepre."stock_type` where `tid` = '".$v."'");
				if($nav[0][1]<>null){
					loop($nav[0][1]);
					}
				echo '<a href="?tid='.$nav[0][0].'">'.$nav[0][2].'</a> => ';
				}
			
			}
	return ' '.loop($nav[0][1]).' '.$current;
	}
	//导航
function read_order_item($t,$iid,$uid){
	global $db; global $tablepre;
	if($t==1){
		$check = $db->select("SELECT v2 FROM `".tablepre."session` where uid = '".$uid."' and type = '2' and v = '".$iid."'");
		return $check[0][0];
		}elseif($t==2){
			$check = $db->select("SELECT v2 FROM `".tablepre."session` where uid = '".$uid."' and type = '2' and v = '".$iid."'");
			return $check[0][0];
			
			}elseif($t==3){
				$check = $db->select("SELECT v2 FROM `".tablepre."session` where uid = '".$uid."' and type = '2'");
				for($i=0;$i<count($check);$i++){
					$v += $check[$i][0];
					}
					return $v;
				}

	}
//读商品
function order_total($type,$oid){
	global $db; global $tablepre;
		$order_item = $db->select("SELECT amount,price FROM `".tablepre."order_item` where oid = ".$oid."");
	for($i=0;$i<count($order_item);$i++){
		$order_item_count .= $order_item[$i][0].',';
		$order_price_count .= $order_item[$i][1].',';
		}
		if($type==1){
			return array_sum(explode(',',$order_price_count));
			}else{
				return array_sum(explode(',',$order_item_count));
				}
	}
	//订单统计
	
function pic_return($pic){
	//$h=get_headers($pic);

				if($pic<>null){
				return $pic;
					}else{
					return 'images/nopic.gif';
			}

	}
	//图片返回
function average_price($sid,$userid,$unit){
	global $db; global $tablepre; 
	$item = $db->select("SELECT price FROM `".tablepre."stock_item` where sid = ".$sid."");
	for($i=0;$i<count($item);$i++){
		$price .= $item[$i][0].',';
		}
		if(parameters(11)==0){
			return '<del>'.$unit.mround(array_sum(explode(',',$price))/count($item)).'</del> '.(100-vip($userid,2)).'%'.MEMBER_OFF.' <font color="red">'.$unit.mround((array_sum(explode(',',$price))/count($item))*(vip($userid,2)/100)).'</font>';
			}if(parameters(11)==1){
				return '<del>'.mround(array_sum(explode(',',$price))/count($item)).$unit.'</del> '.(100-vip($userid,2)).'%'.MEMBER_OFF.' <font color="red">'.mround((array_sum(explode(',',$price))/count($item))*(vip($userid,2)/100)).$unit.'</font>';
				}
		
	}
//平均价
function module_check($shop_type){
	global $db; global $tablepre;
		  eval("\$shop = ".$shop_type);
	  foreach($shop as $value){
		  $module_check = $db->select("SELECT name,active FROM `".tablepre."module` where name = '".$value."'");
		  if($module_check[0][1]==1){
			  return $module_check[0][0];
			  }
		}
	
	
	}
//模块检查
function stock_check($value,$stock){
	global $db; global $tablepre;
	if($stock<>null){$arr.= '<option value="'.$stock.'" selected="selected">'.$stock.'</option>';}
		  eval("\$stock = ".$value);
			foreach($stock as $value){
				$arr.='<option value="'.$value.'">'.$value.'</option>';
				}
	return $arr;
	}
//仓库检查

	function explode_implode_array($action,$value){
		eval("\$str = ".$value);
		if($action==1){
			foreach($str as $v){
				$display .= $v.',';
				}
			$display=substr($display,0,-1);
			}elseif($action==2){
				//echo $value;
					$arr = explode(',',$value);
					//print_r($display);
					$arr = array_diff($arr, array(null));
					//去除数组空值
					foreach($arr as $v){
						$display .= "'".$v."',";
						}
						$display='array('.addslashes(substr($display,0,-1)).');';
					}
					return $display;
		}
		//拆分合并mysql里的array 字符
		
			function count_item_quantity($type,$sid){
				global $db; global $tablepre;
		if($type==1){
			$count = $db->select("select SUM(quantity) sum from `".tablepre."stock_item` where `sid` = '".$sid."'");
							if($count[0][0]<1){
		$db->UPDATE("UPDATE `".tablepre."stock` SET `onsale`='0' WHERE (`sid`='".$sid."')");
		}else{
			$db->UPDATE("UPDATE `".tablepre."stock` SET `onsale`='1' WHERE (`sid`='".$sid."')");
			}
			
			}elseif($type==2){
				$count = $db->select("select sid from `".tablepre."stock_item` group by sid");
				
				for($i=0;$i<count($count);$i++) {
					count_item_quantity(1,$count[$i][0]);
					$sid_arr.=$count[$i][0].',';
					}
					$e_not_in = array_filter(explode(',',$sid_arr));
					$i_not_in=implode(',',$e_not_in);
					$db->UPDATE("UPDATE `".tablepre."stock` SET `onsale`='0' WHERE sid NOT IN (".$i_not_in.")");
				
				}
			

		}
		//商品上下架数量检查
		
		function back_to_stock($oid,$uid){
	global $db; global $tablepre; 
	//$db->update("UPDATE `".tablepre."order` SET `situation`='1' WHERE (`oid`='".$oid."') ");
	$order_item = $db->select("SELECT amount,item,uid FROM `".tablepre."order_item` where oid = '".$oid."'");
	if($order_item[0][2]<>$uid){
		exit('<script>alert("'.SITE_ERROR.'"); javascript:history.go(-1);</script>');
		}
	for($i=0;$i<count($order_item);$i++){
		//echo $order_item[$i][1].'-';
		$db->UPDATE("UPDATE `".tablepre."stock_item`  set quantity = concat(quantity + ".$order_item[$i][0].") where iid = ".$order_item[$i][1]."");
		}
		$db->update("UPDATE `".tablepre."order` SET `situation`='1' WHERE (`oid`='".$oid."') ");
	}
	//返回商品到库存
	
			function back_to_stock_all(){
	global $db; global $tablepre; 
	$now_time = strtotime(date('Y-m-d H:i:s'));
	$order = $db->select("SELECT oid,datetime FROM `".tablepre."order` where `paid` = '0' and `delete` = '0' and `situation`='0'");
	if(count($order)>=1){
	for($i=0;$i<count($order);$i++){
		$sec = $now_time - strtotime($order[$i][1]); 
		$h=floor($sec/3600);
		if($h>parameters(23)){
			$order_arr .= $order[$i][0].',';
			$db->update("UPDATE `".tablepre."order` SET `situation`='1' WHERE (`oid`='".$order[$i][0]."') ");
			$is_go = 1;
			};
		}
		if($is_go<>null){
			$item_arr_e = array_filter(explode(',',$order_arr));
			$item_arr_i = implode(',',$item_arr_e);
			$order_item = $db->select("SELECT item,amount FROM `".tablepre."order_item` where `oid` in (".$item_arr_i.")");
			for($i=0;$i<count($order_item);$i++){
				$db->UPDATE("UPDATE `".tablepre."stock_item`  set quantity = concat(quantity + ".$order_item[$i][1].") where iid = ".$order_item[$i][0]."");
				}}}
	}
	//返回商品到库存-全部
	
	function unit($money){
		global $db; global $tablepre; 
		$unit = $db->select("SELECT value FROM `".tablepre."config` where id = '4'");
		if(parameters(11)==0){
			return $unit[0][0].' '.$money;
			}elseif(parameters(11)==1){
				return $money.' '.$unit[0][0];
				}
		}
		//单位

function current_item_ctock($sid){
	global $db; global $tablepre; 
	$current_item_ctock = $db->select("SELECT SUM(quantity) FROM `".tablepre."stock_item` where sid = '".$sid."'");
	return $current_item_ctock[0][0];

	}
	//当前库存
function stock_control(){
	
	}
	//合理库存
function previous_month_sale($sid){
	global $db; global $tablepre; 
	$previous_month_sale = $db->select("SELECT a.oid,a.amount,b.datetime FROM `".tablepre."order_item` a left join `".tablepre."order` b on (a.oid = b.oid) where a.sid = '".$sid."' and b.paid = 1 and b.situation = 2");
	$lastm = mktime(0,0,0,date("m"),0,date("Y"));
	$twomago = mktime(0,0,0,date("m")-1,0,date("Y"));
	for($i=0;$i<count($previous_month_sale);$i++){
		//$oid_arr .= mktime(0,0,0,date("m")-1,0,substr($previous_month_sale[$i][2],0,4));
		$sqldate=mktime(0,0,0,substr($previous_month_sale[$i][2],5,7),substr($previous_month_sale[$i][2],8,10),substr($previous_month_sale[$i][2],0,4));
		if($sqldate>$twomago and $sqldate<$lastm){
			$amount .= $previous_month_sale[$i][1].',';
			}
			
		}
		
		return array_sum(explode(',',$amount));
		//echo date("Y/m/d",$lastm).'上个月'.date("Y/m/d",$twomago).'上两个月';
		// 上一个月return date("Y/m/d", $nowm);
		//return data
	//$previous_month_sale = $db->select("SELECT SUM(amount) FROM `".tablepre."order_item` a left join `".tablepre."order` b on (a.oid = b.oid) where a.sid = '".$sid."'");
	//print_r($previous_month_sale);
	}
	//上月销售
	
	function minimun($sid){
	global $db; global $tablepre;
	$list = $db->select("SELECT value FROM `".tablepre."minimun` where sid = '".$sid."' limit 1");
	return $list[0][0];
	}
	//最少库存
?>
